/* 
Try to rename a series of files
*/ 
#include"string.h"
#include"stdio.h"
#include"stdlib.h"

int main(int argc,char *argv[])
{
  int starNumOld, starNumNew, questionNumOld, questionNumNew; int i, starSequence[20], questionSequence[20];
  char line[200],str[200],tmpstr[20],*p1,*p2;
  char tmpchar;
  if(argc!=3)
  {
    printf("\n Usage examples:");      
    printf("\n          mmv  \"*.OLD\"  \"*.NEW\" ");      
    printf("\n          mmv  \\*.OLD  \\*.NEW ");      
    printf("\n          mmv  \\*Very\\*Ugly'?'  \\?Beautiful\\*");      
    printf("\n          mmv  My\\*/Year1999\\*  My\\*/Old\\*\n\n");
    printf(" mmv will multi-move a series of old files to new names if \\*\n");
    printf(" or '*' is used in stead of * (same for ?). It will substitute\n");
    printf(" \\* and \\? in their own appearing sequences specifically.\n\n");
    printf(" Attention:\n");
    printf("   Though I call 'mv -i', which makes mmv does not overwrite\n");
    printf("   existing files on SGI (except some trivial informations),\n");
    printf("   but it DOES OVERWRITE existing files on SOLARIS, and no\n");
    printf("   other systems have been tested yet. This is a bug I can't.\n");
    printf("   solve now. Use it with care.\n\n");
    printf(" Version:\n   0.0.1	04/27/2000\n   0.0.2	09/01/2000\n");
    printf("   In 0.0.2, a bug about file name having path (i.e., with '/'\n");
    printf("   character), was corrected.\n\n");
    printf("                          --- xucs007@qq.com 2025/9/3\n\n");
    exit(0); 
  }

  p1=argv[1];
  p2=argv[2];

  strcpy(line,"ls -d1 ");
  strcat(line,p1);
  strcat(line," | sed -e 's/");

  starNumOld     = 0;
  questionNumOld = 0;
  
  for(i=0;p1[i]!='\0';i++)
  {
    if(p1[i]=='*')
    { 
       strcat(line,"\\(.*\\)");
       starSequence[ starNumOld ] = starNumOld + questionNumOld;
       starNumOld++;
    }
    else if(p1[i]=='?') 
    { strcat(line,"\\(.\\)");
       questionSequence[ questionNumOld ] = starNumOld + questionNumOld;
       questionNumOld++;
    }
    else if(p1[i]=='.') strcat(line,"\\.");
    else if(p1[i]=='/') strcat(line,"\\/");
	/* Thus can include some directorys, say,
	 *     mmv subdir\?/old\* subdir\?/new\*          */
    else                         /* Not '*', not '?', not '.', a normal word */
    { 
       tmpstr[0]=p1[i];
       tmpstr[1]='\0';
       strcat(line,tmpstr);
    }
  }
  if( ( starNumOld + questionNumOld ) >= 20 ) 
  {  printf( " Failed! Too many '*' and '?' ! \n"); exit(0); }
 

  starNumNew     = 0;
  questionNumNew = 0;

  strcpy(str, "$/mv -i & ");
  for(i=0;p2[i]!='\0';i++)
  {
    if(p2[i]=='*') 
    { 
       tmpstr[0]='\\';
       tmpstr[1]=starSequence[starNumNew] + 1 + 48; 
                                                /* change digit to char */
       tmpstr[2]='\0';
       strcat(str,tmpstr);
       starNumNew++;
    }
    else if(p2[i]=='?') 
    { 
       tmpstr[0]='\\';
       tmpstr[1]=questionSequence[ questionNumNew ] + 1 + 48; 
                                                /* change digit to char */
       tmpstr[2]='\0';
       strcat(str,tmpstr);
       questionNumNew++;
    }
    else if(p2[i]=='/') 
    { 
       tmpstr[0]='\\';
       tmpstr[1]='/';
       tmpstr[2]='\0';
       strcat(str,tmpstr);
    }
    else
    { 
       tmpstr[0]=p2[i];
       tmpstr[1]='\0';
       strcat(str,tmpstr);
    }
  }
  if(starNumOld != starNumNew || questionNumOld != questionNumNew ) 
  {  printf( "\n Number of '*' and '?' doesn't match, continue ? (Y/N)"); 
     tmpchar = getchar();
     if( tmpchar == 'y' || tmpchar == 'Y' ) ;  /* Continue */
     else exit(0); 
  }

  strcat(line,str);
  strcat(line, "/'  | sh ");

  system(line);

}
